package com.gongpingjialoan.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.Blogs;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/18.
 */
public class MarqueeView extends ViewFlipper {

    private Context mContext;
    private List<Blogs> notices;
    private boolean isSetAnimDuration = false;
    private OnItemClickListener onItemClickListener;

    private int interval = 3000;
    private int animDuration = 1000;
    private int textColor = 0xffffffff;


    private static final int TEXT_GRAVITY_LEFT = 0, TEXT_GRAVITY_CENTER = 1, TEXT_GRAVITY_RIGHT = 2;

    public MarqueeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.mContext = context;
        if (notices == null) {
            notices = new ArrayList<>();
        }

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MarqueeViewStyle, defStyleAttr, 0);
        interval = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvInterval, interval);
        isSetAnimDuration = typedArray.hasValue(R.styleable.MarqueeViewStyle_mvAnimDuration);
        animDuration = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvAnimDuration, animDuration);
        textColor = typedArray.getColor(R.styleable.MarqueeViewStyle_mvTextColor, textColor);

        typedArray.recycle();

        setFlipInterval(interval);

        Animation animIn = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_in);
        if (isSetAnimDuration) animIn.setDuration(animDuration);
        setInAnimation(animIn);

        Animation animOut = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_out);
        if (isSetAnimDuration) animOut.setDuration(animDuration);
        setOutAnimation(animOut);
    }



    // 根据公告字符串列表启动轮播
    public void startWithList(List<Blogs> notices) {
        setNotices(notices);
        start();
    }



    // 启动轮播
    public boolean start() {
        if (notices == null || notices.size() == 0) return false;
        removeAllViews();
        int size = notices.size();

        for (int i = 0; i < size; i++) {
            View view = createTextView(notices.get(i));
            TextView tv1 = (TextView) view.findViewById(R.id.text1);
            TextView tv2 = (TextView) view.findViewById(R.id.text2);
            tv1.setOnClickListener(myOnClickListener);
            tv2.setOnClickListener(myOnClickListener);
            addView(view);
        }

        if (size > 1) {
            startFlipping();
        }
        return true;
    }

    private OnClickListener myOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(v);
            }
        }
    };

    // 创建ViewFlipper下的TextView
    private View createTextView(Blogs person) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.blog_layout, null);
        TextView tv1 = (TextView) view.findViewById(R.id.text1);
        TextView tv2 = (TextView) view.findViewById(R.id.text2);
        tv1.setText(person.getChildren().get(0).getTitle());
        tv1.setTag(person.getChildren().get(0));
        if(person.getChildren().size() < 2){
            view.findViewById(R.id.main2).setVisibility(View.GONE);
        }else{
            tv2.setText(person.getChildren().get(1).getTitle());
            tv2.setTag(person.getChildren().get(1));
        }
        return view;
    }

    public int getPosition() {
        return (int) getCurrentView().getTag();
    }

    public List<Blogs> getNotices() {
        return notices;
    }

    public void setNotices(List<Blogs> notices) {
        this.notices = notices;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View textView);
    }

}
